package com.dianrui.firemenmonitor.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.firemenmonitor.Constant;
import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.adapter.AlarmPromptAdater;
import com.dianrui.firemenmonitor.bean.Alerm;
import com.dianrui.firemenmonitor.util.OkHttpUtils;
import com.dianrui.firemenmonitor.util.StringUtils;
import com.dianrui.firemenmonitor.util.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AlarmPromptActivity extends BaseActivity {

    private List<Alerm> alermList = new ArrayList<>();
    private AlarmPromptAdater alarmPromptAdater;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;

    @Override
    public int getContentViewId() {
        return R.layout.activity_alarmprompt;
    }

    @Override
    public void initView() {
        customInit(R.color.blue_518bfa);
        title.setText(getString(R.string.alarm_prompt_title));
        alarmPromptAdater = new AlarmPromptAdater(AlarmPromptActivity.this, alermList);
        listview.setAdapter(alarmPromptAdater);
        getAlert();
    }

    private void getAlert() {
        OkHttpUtils.getInstance().postWithMemberToken(Url.WARNING, "", new OkHttpUtils.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("code"))) {
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        Alerm alerm = new Alerm();
                                        JSONObject jsonObject1 = jsonArray.optJSONObject(i);
                                        alerm.inputtime = jsonObject1.optString("inputtime");
                                        alermList.add(alerm);
                                    }
                                    if (alarmPromptAdater != null) {
                                        alarmPromptAdater.refresh(alermList);
                                    }

                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (alermList.size()<=0) {
                        emptyView.setVisibility(View.VISIBLE);
                        listview.setVisibility(View.GONE);
                    }else {
                        emptyView.setVisibility(View.GONE);
                        listview.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
